#include <iostream>
#include <cassert>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;

#define SIZE 1024

int main()
{
    // 1. 创建管道
    int pipefd[2] = { 0 };
    int n = pipe(pipefd);
    assert(n == 0);

    // 2. 创建子进程
    pid_t id = fork();
    if(id)
    {
        // 父进程
        close(pipefd[0]);
        char message[SIZE] = { 0 };
        sprintf(message, "i am father\n");
        write(pipefd[1], message, sizeof(message));
    }
    else
    {
        // 子进程
        close(pipefd[1]);
        char buffer[SIZE] = { 0 };
        read(pipefd[0], buffer, sizeof(buffer) - 1);
        cout << buffer;
    }
    return 0;
}